
#include "../UserFreedom_RsGis_1/include/RsgisPortal.h"

#include "ExtensionManager.h"
#include "ExtensionSession.h"
#include "XmlRpcClient.h"

#include "gdal_priv.h"


RsgisPortal::RsgisPortal(quint16 iPort, QObject *context)
	:m_context(context)
{
	iPortAddress = iPort;
	initSystem();
}

//RsgisPortal::RsgisPortal(quint16 iPort, QVariantList ArgList,QObject *context)
//    :m_context(context)
//{
//	iPortAddress = iPort;
//	QArgList = ArgList;
//	initSystem();
// }

void RsgisPortal::initSystem()
{
	//read ini file
	ExtensionSession* es = ExtensionSession::instance();
	ExtensionManager* em = ExtensionManager::instance();
	em->initExtension("ExtensionConfig.ini");

	//init extension system
	//QString rpcServer = "http://localhost:8080/RPC2";
	QString rpcFreeServer = em->getFreeRpcInfo();
	XmlRpcClient* rpcFreeClient = new XmlRpcClient();
	//rpcFreeClient->initRpc(rpcFreeServer);
	rpcFreeClient->initRpc(iPortAddress);

	//char* sPathTifIn;
	//QByteArray baIn = QTifPathIn.toLatin1();    // must
	//sPathTifIn = baIn.data();
	//char* sPathTifOut;
	//QByteArray baOut = QTifPathOut.toLatin1();    // must
	//sPathTifOut = baOut.data();

	//rpcFreeClient->sTifPathIn = QTifPathIn;
	//rpcFreeClient->sTifPathOut = QTifPathOut;
	es->addObject("XmlRpcFreeClient", rpcFreeClient);

	//QString rpcServer = "http://localhost:8081/RPC2";
	//QString rpcWorkServer = em->getWorkRpcInfo();
	//XmlRpcClient* rpcWorkClient = new XmlRpcClient();
	//rpcWorkClient->initRpc(rpcWorkServer);
	//es->addObject("XmlRpcWorkClient", rpcWorkClient);

	mainSystem();
	em->loadExtension();

	//goAhead();
}

void RsgisPortal::mainSystem()
{
	//load main windows
}

void RsgisPortal::goAhead(QVariantList QArgList)
{
	ExtensionSession* es = ExtensionSession::instance();
	ExtensionManager* em = ExtensionManager::instance();

	QObject* obj = es->getObject("XmlRpcFreeClient");
	XmlRpcClient* rpcFreeClient = (XmlRpcClient*)(obj);

	QList<QVariant> SysParaList = QArgList[0].value<QList<QVariant>>();
	QList<QVariant> AlgParaList = QArgList[1].value<QList<QVariant>>();

	SysParaList << QVariant(iPortAddress);

	QArgList.clear();
	QArgList << (QVariant)SysParaList;
	QArgList << (QVariant)AlgParaList;

	rpcFreeClient->doMasterFunction("Master", "CallServer", QArgList);
	//load main windows
	//connect(this, SIGNAL(openDlg()), this, SLOT(onOpenDlg()),Qt::QueuedConnection);
}

void RsgisPortal::CallResult()
{
	ExtensionSession* es = ExtensionSession::instance();
	ExtensionManager* em = ExtensionManager::instance();

	QObject* obj = es->getObject("XmlRpcFreeClient");
	XmlRpcClient* rpcFreeClient = (XmlRpcClient*)(obj);

	rpcFreeClient->doResultFunction("Master", "CallforResult");
}

//void RsgisPortal::CallforResult()
//{
//	ExtensionSession* es = ExtensionSession::instance();
//	ExtensionManager* em = ExtensionManager::instance();
//
//	QObject* obj = es->getObject("XmlRpcFreeClient");
//	XmlRpcClient* rpcFreeClient = (XmlRpcClient*)(obj);
//
//	while (!rpcFreeClient->MasterArg.isValid())
//	{
//		rpcFreeClient->doResultFunction("Master", "CallforResult");
//		_sleep(1000);
//	}
//}

void RsgisPortal::handleEvent(const QObject& event)
{
    //if(event.getTopic() == "zhimakaimen")
    //{
    //    emit openDlg();
    //}

}

void RsgisPortal::onChangeLanguage()
{
}

